import { ApiAuth } from '@2/app/core/api/api.auth';
import { LoginModel } from '@2/typings/model/login.model';
import { ErrorMessageMode } from '#/axios';
import { Result } from '@2/typings/http.d';
import { Enums } from '@2/app/core/api/enums';
import { defHttp } from '@/utils/http/axios';
import { Oauth2Token } from '@2/typings/oauth2';
import { injectable } from 'inversify';
import { CLIENT_ID, CLIENT_SECRET } from '@2/typings/constant/api.c';
import { Base64Util } from '@2/app/core/utils/base64.util';

/**
 * @program: microservice-central-portal
 * @description: 获取授权接口定义实现
 * @author: baiHoo.chen
 * @create: 2024-04-07
 **/
@injectable()
export class ApiAuthImpl implements ApiAuth {
  login(params: LoginModel, mode: ErrorMessageMode): Promise<Oauth2Token> {
    return defHttp.post<Oauth2Token>(
      {
        url: Enums.OAUTH_TOKEN,
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          Authorization: 'Basic ' + Base64Util.encode(CLIENT_ID + ':' + CLIENT_SECRET),
        },
        params,
      },
      { errorMessageMode: mode },
    );
  }
  logout(): Promise<void> {
    return defHttp.get<void>({ url: Enums.LOGOUT });
  }
  refresh(refreshToken: string): Promise<Result> {
    return defHttp.post<Result>({ url: Enums.OAUTH_TOKEN, params: { refreshToken } });
  }

  getCaptcha(deviceId: string): Promise<string> {
    return defHttp.get<string>({ url: Enums.RANDOM_CODE + deviceId });
  }
}
